/*
 * ObjectStateObserver.cpp
 *
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses 
 */

#include "ObjectStateObserver.h"
#include "ModelConstants.h"
#include "MapObject.h"
#include <iostream>
#include "../common/Utils.h"

ObjectStateObserver::ObjectStateObserver(Partida& game_room)
    : game_room(game_room) {

}

void ObjectStateObserver::update(Event event, void* entity, void* other) {
    if (event == OBJECT_DESTROYED) {
        MapObject* object = (MapObject*) entity;
        /************************LOG*******************************/
        int id = object->getId();
        int alignment = object->getAlignment();
        if (alignment == AL_PLAYER) {
            std::cout << "Bomberman " << id;
        } else if (alignment == AL_NEUTRAL) {
            std::cout << "Neutral " << id;
        } else {
            std::cout << "Enemigo " << id;
        }
        std::cout << " destruido";
        if (other != 0) {
            MapObject* owner = (MapObject*) other;
            std::cout << " por el objeto " << owner->getId();
        }
        std::cout << "\n";
        /**********************************************************/
        Message message;
        message.code = DELETE;
        message.id = object->getId();
        game_room.notifyPlayers(message);
    } else if (event == OBJECT_MOVED) {
        MapObject* object = (MapObject*) entity;
        /************************LOG*******************************/
        int id = object->getId();
        int x = object->getX();
        int y = object->getY();
        int alignment = object->getAlignment();
        if (alignment == AL_PLAYER) {
            std::cout << "Bomberman " << id;
        } else if (alignment == AL_NEUTRAL) {
            std::cout << "Neutral " << id;
        } else {
            std::cout << "Enemigo " << id;
        }
        std::cout << " está en (" << x << "," << y << ")\n";
        /**********************************************************/
        Message message;
        message.code = UPDATE;
        message.id = object->getId();
        message.args.push_back(Utils::intToString(object->getX()));
        message.args.push_back(Utils::intToString(object->getY()));
        game_room.notifyPlayers(message);
    }
}
